﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CDoDNSessionScheduler
{
    public partial class Session : UserControl
    {
        #region Properties
        private enum AnimationStateValues
        {
            Closed,
            Opening,
            Open,
            Closing
        }
        private AnimationStateValues AnimationState;
        private bool _selected;
        public bool Selected
        {
            get { return _selected; }
            set
            {
                if (value)
                {
                    //Mark other sessions in same timeslot as not selected
                    foreach (object raw in GlobalVariables.Get().MainSessionList.Children)
                    {
                        //we wrap this in a try/catch as there are non Session objects in the grid
                        try
                        {
                            if (((Session)raw).TimeSlot == this.TimeSlot) ((Session)raw).Selected = false;
                        }
                        catch { }
                    }

                    //Mark this session as the selected one and add it to the list
                    SelectedBox.Visibility = Visibility.Visible;
                    ((ScheduledItem)GlobalVariables.Get().MainChecklist.Children[TimeSlot]).SessionName.Text = Title;

                    if (_selected != value)
                    {
                        // save this session to the user's schedule
                        GlobalVariables.Get().UserSchedule.Tracks[TimeSlot - 1] = Track.Id;
                        GlobalVariables.Get().UserSchedule.Save();
                    }
                }
                else
                {
                    //remove selection mark from session and remove it from the list
                    SelectedBox.Visibility = Visibility.Collapsed;
                    ((ScheduledItem)GlobalVariables.Get().MainChecklist.Children[TimeSlot]).SessionName.Text = string.Empty;

                    if (_selected != value)
                    {
                        // remove this session from the user's schedule
                        GlobalVariables.Get().UserSchedule.Tracks[TimeSlot - 1] = 0;
                        GlobalVariables.Get().UserSchedule.Save();
                    }
                }
                _selected = value;
            }
        }
        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                this.SessionTitle.Text = _title;
                this.SessionTitle_small.Text = _title;
            }
        }
        private string _author;
        public string Author
        {
            get { return _author; }
            set
            {
                _author = value;
                this.SessionPresenterName.Text = _author;
            }
        }
        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                this.SessionDescription.Text = _description;
            }
        }
        private ScheduleTrack _track;
        public ScheduleTrack Track
        {
            get { return _track; }
            set
            {
                _track = value;

                this.SessionBack.Fill.SetValue(SolidColorBrush.ColorProperty, _track.BackColor);
                this.SessionBack_Small.Fill.SetValue(SolidColorBrush.ColorProperty, _track.BackColor);
            }

        }
        private string _bio;
        public string Bio
        {
            get { return _bio; }
            set
            {
                _bio = value;
            }
        }
        public int TimeSlot { get; set; }

        private DispatcherTimer openTimer;
        #endregion

        public Session()
        {
            InitializeComponent();

            this.GetBig.Completed += new EventHandler(GetBig_Completed);
            this.GetSmall.Completed += new EventHandler(GetSmall_Completed);
            openTimer = new DispatcherTimer();
            AnimationState = AnimationStateValues.Closed;
            Selected = false;
        }

        #region Animation Handlers
        void GetSmall_Completed(object sender, EventArgs e)
        {
            AnimationState = AnimationStateValues.Closed;
        }

        public void BeginGetBig(object sender, EventArgs e)
        {
            AnimationState = AnimationStateValues.Opening;
            this.GetBig.Begin();
            openTimer.Stop();
        }

        void GetBig_Completed(object sender, EventArgs e)
        {
            AnimationState = AnimationStateValues.Open;
        }

        private void Session_MouseEnter(object sender, MouseEventArgs e)
        {
            openTimer.Interval = new TimeSpan(0, 0, 0, 0, 50);
            openTimer.Tick += new EventHandler(BeginGetBig);
            openTimer.Start();
        }

        private void Session_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Selected)
            {
                Selected = false;
            }
            else
            {
                Selected = true;
            }
                
        }

        private void Session_MouseLeave(object sender, MouseEventArgs e)
        {
            switch (AnimationState)
            {
                case AnimationStateValues.Opening:
                    this.GetBig.Stop();
                    openTimer.Stop();
                    break;
                case AnimationStateValues.Open:
                    this.GetSmall.Begin();
                    AnimationState = AnimationStateValues.Closing;
                    break;
                case AnimationStateValues.Closed:
                    openTimer.Stop();
                    break;
                default:
                    break;
            }

        }
        #endregion
    }
}
